﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;
using GoShopping.Model.Enumerations;
using System.Data;

namespace GoShopping.BLL
{
    /// <summary>
    /// 邮件模板操作相关
    /// </summary>
    public class EmailTempleteBean
    {
        /// <summary>
        /// 更新一个邮件模板,如果不存在,则创建
        /// </summary>
        /// <param name="email"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool UpdateEmailTemplete(EmailTemplete email,EmailTempleteType type)
        {
            int templateCount =
                Databases.DBGoShopping.Select(GoShoppingNBear.EmailTemplete,GoShoppingNBear.EmailTemplete.Name.Count()).Where(GoShoppingNBear.EmailTemplete.Name == type.ToString()).ToScalar<int>();
            if (templateCount == 1)
            {
                //更新
                int i = Databases.DBGoShopping.Update(GoShoppingNBear.EmailTemplete).AddColumn(
                    GoShoppingNBear.EmailTemplete.ConfigExplain, email.ConfigExplain).AddColumn(
                    GoShoppingNBear.EmailTemplete.ContentHTML, email.ContentHTML).AddColumn(
                    GoShoppingNBear.EmailTemplete.EmailTitle, email.EmailTitle).AddColumn(
                    GoShoppingNBear.EmailTemplete.IsInUse, email.IsInUse).AddColumn(
                    GoShoppingNBear.EmailTemplete.Name, type.ToString()).Where(GoShoppingNBear.EmailTemplete.Name == type.ToString()).Execute();
                if (i > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (templateCount < 1)
                {
                    //插入
                    int i = Databases.DBGoShopping.Insert(GoShoppingNBear.EmailTemplete).AddColumn(
                        GoShoppingNBear.EmailTemplete.ConfigExplain, email.ConfigExplain).AddColumn(
                        GoShoppingNBear.EmailTemplete.ContentHTML, email.ContentHTML).AddColumn(
                        GoShoppingNBear.EmailTemplete.EmailTitle, email.EmailTitle).AddColumn(
                        GoShoppingNBear.EmailTemplete.IsInUse, email.IsInUse).AddColumn(
                        GoShoppingNBear.EmailTemplete.Name, type.ToString()).Execute();
                    if (i > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    throw (new Exception("EmailTempleteName is not single!"));
                }
            }
        }

        /// <summary>
        /// 获取一个EmailTemplete
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public EmailTemplete GetEmailTemplete(EmailTempleteType type)
        {
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.EmailTemplete).Where(GoShoppingNBear.EmailTemplete.Name == type.ToString()).ToDataSet().Tables[0];
            if (dt.Rows.Count == 1)
            {
                EmailTemplete e = NBear.Mapping.ObjectConvertor.ToObject<DataRow, EmailTemplete>(dt.Rows[0]);
                return e;
            }
            else
            {
                if (dt.Rows.Count > 1)
                {
                    throw (new Exception("EmailTemplete is not single!"));
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有EmailTemplete
        /// </summary>
        /// <returns></returns>
        public IList<EmailTemplete> GetAllEmailTemplete()
        {
            IList<EmailTemplete> emailList = new List<EmailTemplete>();
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.EmailTemplete).ToDataSet().Tables[0];
            EmailTemplete[] userArr = NBear.Mapping.ObjectConvertor.ToArray<DataTable, EmailTemplete>(dt);

            foreach (EmailTemplete u in userArr)
            {
                emailList.Add(u);
            }
            return emailList;
        }
    }
}
